﻿using System.Collections.Generic;
using XLua;
#if USE_UNI_LUA
using LuaAPI = UniLua.Lua;
using RealStatePtr = UniLua.ILuaState;
using LuaCSFunction = UniLua.CSharpFunctionDelegate;
#else
using LuaAPI = XLua.LuaDLL.Lua;
using RealStatePtr = System.IntPtr;
using LuaCSFunction = XLua.LuaDLL.lua_CSFunction;
#endif

namespace XLua.CSObjectWrap
{
    using Utils = XLua.Utils;

    public class UnityEngineResourcesWrap
    {
        public static void __Register(RealStatePtr L)
        {
            ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
            System.Type type = typeof(UnityEngine.Resources);
            Utils.BeginObjectRegister(type, L, translator, 0, 0, 0, 0);

            Utils.EndObjectRegister(type, L, translator, null, null, null, null, null);

            Utils.BeginClassRegister(type, L, __CreateInstance, 10, 0, 0);
            Utils.RegisterFunc(
                L,
                Utils.CLS_IDX,
                "FindObjectsOfTypeAll",
                _m_FindObjectsOfTypeAll_xlua_st_
            );
            Utils.RegisterFunc(L, Utils.CLS_IDX, "Load", _m_Load_xlua_st_);
            Utils.RegisterFunc(L, Utils.CLS_IDX, "LoadAsync", _m_LoadAsync_xlua_st_);
            Utils.RegisterFunc(L, Utils.CLS_IDX, "LoadAll", _m_LoadAll_xlua_st_);
            Utils.RegisterFunc(
                L,
                Utils.CLS_IDX,
                "GetBuiltinResource",
                _m_GetBuiltinResource_xlua_st_
            );
            Utils.RegisterFunc(L, Utils.CLS_IDX, "UnloadAsset", _m_UnloadAsset_xlua_st_);
            Utils.RegisterFunc(
                L,
                Utils.CLS_IDX,
                "UnloadUnusedAssets",
                _m_UnloadUnusedAssets_xlua_st_
            );
            Utils.RegisterFunc(
                L,
                Utils.CLS_IDX,
                "InstanceIDToObject",
                _m_InstanceIDToObject_xlua_st_
            );
            Utils.RegisterFunc(
                L,
                Utils.CLS_IDX,
                "InstanceIDToObjectList",
                _m_InstanceIDToObjectList_xlua_st_
            );

            Utils.EndClassRegister(type, L, translator);
        }

        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int __CreateInstance(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
                if (LuaAPI.lua_gettop(L) == 1)
                {
                    var gen_ret = new UnityEngine.Resources();
                    translator.Push(L, gen_ret);

                    return 1;
                }
            }
            catch (System.Exception gen_e)
            {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            return LuaAPI.luaL_error(L, "invalid arguments to UnityEngine.Resources constructor!");
        }

        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _m_FindObjectsOfTypeAll_xlua_st_(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

                {
                    System.Type _type = (System.Type)
                        translator.GetObject(L, 1, typeof(System.Type));

                    var gen_ret = UnityEngine.Resources.FindObjectsOfTypeAll(_type);
                    translator.Push(L, gen_ret);

                    return 1;
                }
            }
            catch (System.Exception gen_e)
            {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
        }

        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _m_Load_xlua_st_(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

                int gen_param_count = LuaAPI.lua_gettop(L);

                if (
                    gen_param_count == 1
                    && (LuaAPI.lua_isnil(L, 1) || LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TSTRING)
                )
                {
                    string _path = LuaAPI.lua_tostring(L, 1);

                    var gen_ret = UnityEngine.Resources.Load(_path);
                    translator.Push(L, gen_ret);

                    return 1;
                }
                if (
                    gen_param_count == 2
                    && (LuaAPI.lua_isnil(L, 1) || LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TSTRING)
                    && translator.Assignable<System.Type>(L, 2)
                )
                {
                    string _path = LuaAPI.lua_tostring(L, 1);
                    System.Type _systemTypeInstance = (System.Type)
                        translator.GetObject(L, 2, typeof(System.Type));

                    var gen_ret = UnityEngine.Resources.Load(_path, _systemTypeInstance);
                    translator.Push(L, gen_ret);

                    return 1;
                }
            }
            catch (System.Exception gen_e)
            {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }

            return LuaAPI.luaL_error(L, "invalid arguments to UnityEngine.Resources.Load!");
        }

        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _m_LoadAsync_xlua_st_(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

                int gen_param_count = LuaAPI.lua_gettop(L);

                if (
                    gen_param_count == 1
                    && (LuaAPI.lua_isnil(L, 1) || LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TSTRING)
                )
                {
                    string _path = LuaAPI.lua_tostring(L, 1);

                    var gen_ret = UnityEngine.Resources.LoadAsync(_path);
                    translator.Push(L, gen_ret);

                    return 1;
                }
                if (
                    gen_param_count == 2
                    && (LuaAPI.lua_isnil(L, 1) || LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TSTRING)
                    && translator.Assignable<System.Type>(L, 2)
                )
                {
                    string _path = LuaAPI.lua_tostring(L, 1);
                    System.Type _type = (System.Type)
                        translator.GetObject(L, 2, typeof(System.Type));

                    var gen_ret = UnityEngine.Resources.LoadAsync(_path, _type);
                    translator.Push(L, gen_ret);

                    return 1;
                }
            }
            catch (System.Exception gen_e)
            {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }

            return LuaAPI.luaL_error(L, "invalid arguments to UnityEngine.Resources.LoadAsync!");
        }

        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _m_LoadAll_xlua_st_(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

                int gen_param_count = LuaAPI.lua_gettop(L);

                if (
                    gen_param_count == 1
                    && (LuaAPI.lua_isnil(L, 1) || LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TSTRING)
                )
                {
                    string _path = LuaAPI.lua_tostring(L, 1);

                    var gen_ret = UnityEngine.Resources.LoadAll(_path);
                    translator.Push(L, gen_ret);

                    return 1;
                }
                if (
                    gen_param_count == 2
                    && (LuaAPI.lua_isnil(L, 1) || LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TSTRING)
                    && translator.Assignable<System.Type>(L, 2)
                )
                {
                    string _path = LuaAPI.lua_tostring(L, 1);
                    System.Type _systemTypeInstance = (System.Type)
                        translator.GetObject(L, 2, typeof(System.Type));

                    var gen_ret = UnityEngine.Resources.LoadAll(_path, _systemTypeInstance);
                    translator.Push(L, gen_ret);

                    return 1;
                }
            }
            catch (System.Exception gen_e)
            {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }

            return LuaAPI.luaL_error(L, "invalid arguments to UnityEngine.Resources.LoadAll!");
        }

        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _m_GetBuiltinResource_xlua_st_(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

                {
                    System.Type _type = (System.Type)
                        translator.GetObject(L, 1, typeof(System.Type));
                    string _path = LuaAPI.lua_tostring(L, 2);

                    var gen_ret = UnityEngine.Resources.GetBuiltinResource(_type, _path);
                    translator.Push(L, gen_ret);

                    return 1;
                }
            }
            catch (System.Exception gen_e)
            {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
        }

        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _m_UnloadAsset_xlua_st_(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

                {
                    UnityEngine.Object _assetToUnload = (UnityEngine.Object)
                        translator.GetObject(L, 1, typeof(UnityEngine.Object));

                    UnityEngine.Resources.UnloadAsset(_assetToUnload);

                    return 0;
                }
            }
            catch (System.Exception gen_e)
            {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
        }

        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _m_UnloadUnusedAssets_xlua_st_(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

                {
                    var gen_ret = UnityEngine.Resources.UnloadUnusedAssets();
                    translator.Push(L, gen_ret);

                    return 1;
                }
            }
            catch (System.Exception gen_e)
            {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
        }

        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _m_InstanceIDToObject_xlua_st_(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

                {
                    int _instanceID = LuaAPI.xlua_tointeger(L, 1);

                    var gen_ret = UnityEngine.Resources.InstanceIDToObject(_instanceID);
                    translator.Push(L, gen_ret);

                    return 1;
                }
            }
            catch (System.Exception gen_e)
            {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
        }

        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _m_InstanceIDToObjectList_xlua_st_(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

                {
                    Unity.Collections.NativeArray<int> _instanceIDs;
                    translator.Get(L, 1, out _instanceIDs);
                    System.Collections.Generic.List<UnityEngine.Object> _objects =
                        (System.Collections.Generic.List<UnityEngine.Object>)
                            translator.GetObject(
                                L,
                                2,
                                typeof(System.Collections.Generic.List<UnityEngine.Object>)
                            );

                    UnityEngine.Resources.InstanceIDToObjectList(_instanceIDs, _objects);

                    return 0;
                }
            }
            catch (System.Exception gen_e)
            {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
        }
    }
}
